package com.sj.uabrowser.mvp.main.view.adp;

import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.orhanobut.logger.Logger;
import com.sj.uabrowser.R;
import com.sj.uabrowser.app.Constant;
import com.sj.uabrowser.app.SPUtil;
import com.sj.uabrowser.app.Util;
import com.sj.uabrowser.mvp.main.bean.News;

import java.util.List;

public class NewsAdp extends BaseMultiItemQuickAdapter<News, BaseViewHolder> {

    public static final int TYPE_LEVEL_0 = 0;
    public static final int TYPE_LEVEL_1 = 1;
    public static final int TYPE_LEVEL_2 = 2;
    public static final int TYPE_LEVEL_3 = 3;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public NewsAdp(List<News> data) {
        super(data);
        addItemType(TYPE_LEVEL_0, R.layout.item_news_0);
        addItemType(TYPE_LEVEL_1, R.layout.item_news_1);
//        addItemType(TYPE_LEVEL_2, R.layout.item_fltr_button);
//        addItemType(TYPE_LEVEL_3, R.layout.item_fltr_price);
    }

    @Override
    protected void convert(BaseViewHolder helper, News item) {
        int fontSize = SPUtil.get(mContext,SPUtil.KEY_FONTSIZE, Constant.FONTSIZE_NORMAL);
        TextView tvFontSize = helper.getView(R.id.tv_topic);
        tvFontSize.setTextSize(fontSize);
        Logger.e("fontSize:"+fontSize);
        ImageView[] miniimgViews = {
                helper.getView(R.id.mini01),
                helper.getView(R.id.mini02),
                helper.getView(R.id.mini03),
        };
        helper.setText(R.id.tv_hot, item.getUpdate_time());
        helper.setVisible(R.id.tv_ad, item.isIs_ad());

        helper.setText(R.id.tv_topic, item.getTitle());
//        TextView tvTopic = helper.getView(R.id.tv_topic);
//        if (item.isRead()) {
//            tvTopic.setTextColor(mContext.getResources().getColor(R.color.color_darker_gray));
//        } else {
//            tvTopic.setTextColor(mContext.getResources().getColor(R.color.item_title_color));

//        }
        helper.setText(R.id.tv_source, item.getSource());
//        boolean isHot = TextUtils.equals("1", item.getHotnews());
//        helper.getView(R.id.tv_hot).setVisibility(isHot ? View.VISIBLE : View.GONE);
        List<News.ImagesBean> miniimgs = item.getImages();
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.drawable.common_list_item_nodata)
                .error(R.mipmap.ic_error)
                .override(miniimgViews[0].getWidth(), miniimgViews[0].getWidth() * 3 / 4);
        switch (helper.getItemViewType()) {
            case TYPE_LEVEL_0:
                int size = miniimgs.size() > 3 ? 3 : miniimgs.size();
                for (int i = 0; i < size; i++) {
                    News.ImagesBean miniimgBean = miniimgs.get(i);
                        Glide.with(mContext)
                                .load(miniimgBean.getUrl())
                                .apply(options)
                                .into(miniimgViews[i]);
                }
                break;
            case TYPE_LEVEL_1:
                News.ImagesBean miniimgBean = miniimgs.get(0);
                    Glide.with(mContext)
                            .load(miniimgBean.getUrl())
                            .apply(options)
                            .into(miniimgViews[0]);
                break;
        }
    }
}
